<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:maintenance:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['project:maintenance:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="maintenanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="auditId" />
      <el-table-column label="ICT专案名称" align="center" prop="ictProjectCode" />
      <el-table-column label="专案区域" align="center" prop="projectArea" />
      <el-table-column label="功能间" align="center" prop="labName" />
      <el-table-column label="L2终端数量" align="center" prop="l2Terminals" />
      <el-table-column label="L3终端数量" align="center" prop="l3Terminals" />
      <el-table-column label="L4终端数量" align="center" prop="l4Terminals" />
      <el-table-column label="其他电子设备" align="center" prop="otherElectronicEquipment" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['project:maintenance:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['project:maintenance:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改稽核专案维护对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="ICT专案名称" prop="ictProjectCode">
              <el-select v-model="form.ictProjectCode" filterable allow-create placeholder="请选择ICT专案名称"
                          clearable @change="ictProjectCodeChange" :style="{width: '90%'}">
                  <el-option v-for="(item, index) in ictProjectCodeOptions" :key="index" :label="item"
                            :value="item" ></el-option>
                </el-select>   
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="专案区域" prop="projectArea">
              <el-select v-model="form.projectArea" filterable allow-create placeholder="请选择专案区域"
                          clearable @change="labNameChange" :style="{width: '90%'}">
                  <el-option v-for="(item, index) in projectAreaOptions" :key="index" :label="item"
                            :value="item" ></el-option>
                </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="功能间" prop="labName">
              <el-select v-model="form.labName" filterable allow-create placeholder="请选择功能间" 
                          clearable :style="{width: '90%'}">
                <el-option v-for="(item, index) in labNameOptions" :key="index" :label="item"
                            :value="item" ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工号" prop="jobId">
              <el-input v-model="form.jobId" placeholder="请输入工号" :style="{width: '90%'}" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">提 交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMaintenance, getMaintenance, delMaintenance, addMaintenance, updateMaintenance, exportMaintenance, MaintenanceOption, MaintenanceboxOption, MaintenancelebOption} from "@/api/project/maintenance";

export default {
  name: "Maintenance",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 稽核专案维护表格数据
      maintenanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      xx: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        jobId: null,
        ictProjectCode: null,
        projectArea: null,
        labName: null,
        l2Terminals: null,
        l3Terminals: null,
        l4Terminals: null,
        otherElectronicEquipment: null,
        maintainerId: null,
        maintenanceTime: null,
        updateId: null,
        updateTime: null,
        reserved: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ictProjectCode: [
          { required: true, message: "请选择ICT专案名称", trigger: "blur" }
        ],
        projectArea: [
          { required: true, message: "请选择专案区域", trigger: "blur" }
        ],
        labName: [
          { required: true, message: "请选择功能间", trigger: "blur" }
        ],
        jobId: [
          { required: true, message: "工号不能为空", trigger: "blur" }
        ],
        l2Terminals: [
          { required: true, message: "L2终端数量不能为空", trigger: "blur" }
        ],
        l3Terminals: [
          { required: true, message: "L3终端数量不能为空", trigger: "blur" }
        ],
        l4Terminals: [
          { required: true, message: "L4终端数量不能为空", trigger: "blur" }
        ],
        maintainerId: [
          { required: true, message: "维护人工号不能为空", trigger: "blur" }
        ],
        maintenanceTime: [
          { required: true, message: "维护时间不能为空", trigger: "blur" }
        ],
        updateId: [
          { required: true, message: "更新人工号不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ],
      },
      
      ictProjectCodeOptions: [],
      projectAreaOptions: [],
      labNameOptions: []
    };
  },
  created() {
    this.getList();
    this.getaictProjectCodeList();
  
  },
  methods: {
    /** 查询稽核专案维护列表 */
    getList() {
      this.loading = true;
      listMaintenance(this.queryParams).then(response => {
        this.maintenanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        auditId: null,
        jobId: null,
        ictProjectCode: null,
        projectArea: null,
        labName: null,
        l2Terminals: null,
        l3Terminals: null,
        l4Terminals: null,
        otherElectronicEquipment: null,
        maintainerId: null,
        maintenanceTime: null,
        updateId: null,
        updateTime: null,
        reserved: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.auditId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    getaictProjectCodeList() {
      this.loading = true;
      MaintenanceOption().then(response => { 
        console.log(response)
        for(var i = 0;i<response.length;i ++){
          this.ictProjectCodeOptions.push(response[i].ictCode);
        }
        for (var i = 0; i < this.ictProjectCodeOptions.length;i ++) {
            for (let j = i + 1; j < this.ictProjectCodeOptions.length;j ++) {
                if (this.ictProjectCodeOptions[i] == this.ictProjectCodeOptions[j]) {
                    this.ictProjectCodeOptions.splice(j, 1);
                    j--;
                }
            }
        }
        this.loading = false;
        console.log(this.ictProjectCodeOptions)
      });
    },
    ictProjectCodeChange(val){
      this.projectAreaOptions = []
       let param = {
         'ictCode':val
       }
       this.xx = val
      MaintenanceboxOption(param).then(response => { 
        for(var i = 0;i<response.length;i ++){
       this.projectAreaOptions.push(response[i].mainRegion);
        }
        for (var i = 0; i < this.projectAreaOptions.length;i ++) {
            for (let j = i + 1; j < this.projectAreaOptions.length;j ++) {
                if (this.projectAreaOptions[i] == this.projectAreaOptions[j]) {
                    this.projectAreaOptions.splice(j, 1);
                    j--;
                }
            }
        }
        this.loading = false;
        console.log(this.projectAreaOptions)
      })
    },

    labNameChange(val){
      this.labNameOptions = []
       let params = {
         'ictCode':this.xx,
         'mainRegion':val
       }
      MaintenancelebOption(params).then(response => { 
        for(var x = 0;x<response.length;x ++){
       this.labNameOptions.push(response[x].approvalPendingLab);
        }
        for (var x = 0; x < this.labNameOptions.length;x ++) {
            for (let y = x + 1; y < this.labNameOptions.length;y ++) {
                if (this.labNameOptions[x] == this.labNameOptions[y]) {
                    this.labNameOptions.splice(y, 1);
                    y--;
                }
            }
        }
        this.loading = false;
        console.log(this.labNameOptions)
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加稽核专案维护";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const auditId = row.auditId || this.ids
      getMaintenance(auditId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改稽核专案维护";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.auditId != null) {
            updateMaintenance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMaintenance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const auditIds = row.auditId || this.ids;
      this.$modal.confirm('是否确认删除稽核专案维护编号为"' + auditIds + '"的数据项？').then(function() {
        return delMaintenance(auditIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有稽核专案维护数据项？').then(() => {
        this.exportLoading = true;
        return exportMaintenance(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
